
package hasthi.metamodel.resource;

import hasthi.common.BaseContext;
import hasthi.common.ChangeListener;
import hasthi.container.UnicastContianier;
import hasthi.coordinator.CoordinatorContext;
import hasthi.manager.ManagerContext;

/**
 * This holds the state for a resource model
 * @author hperera
 */
public class ResourceModelContext {
    private String coordinatorUrl;
    private long cacheForSeconds = 30;
    private UnicastContianier container;
    /**
     * This is used only if we are using the model from 
     * Coordinator
     */
    private final CoordinatorContext coordinatorContext;
    
    private final ManagerContext managerContext;
    
    private final BaseContext baseContext;
    
    private ValueConverter converter;
    

    private final ChangeListener changeListener;
    
    /**
     * 
     * @param managerContext
     */
    public ResourceModelContext(ManagerContext managerContext,ChangeListener changeListener) {
        this.managerContext = managerContext;
        this.container = managerContext.getUnicastContainer();
        this.coordinatorContext = null;
        this.baseContext = managerContext.getBaseContext();
        converter = new ValueConverter();
        this.changeListener = changeListener;
        
    }

    /**
     * 
     * @param coordinatorUrl
     * @param container
     */
    public ResourceModelContext(String coordinatorUrl, final UnicastContianier container,BaseContext baseContext,ChangeListener changeListener) {
        this.coordinatorUrl = coordinatorUrl;
        this.container = container;
        this.coordinatorContext = null;
        this.managerContext = null;
        this.baseContext = baseContext;
        converter = new ValueConverter();
        this.changeListener = changeListener;
    }

    /**
     * This is called when a resource model is created for Coordinator
     * @param container
     * @param coordinatorContext
     */
    public ResourceModelContext(CoordinatorContext coordinatorContext,ChangeListener changeListener) {
        this.container = coordinatorContext.getMyManagerContext().getUnicastContainer();;
        this.coordinatorContext = coordinatorContext;
        this.managerContext = null;
        this.baseContext = coordinatorContext.getMyManagerContext().getBaseContext();
        converter = new ValueConverter();
        this.changeListener = changeListener;
    }
    
    public long getCacheForSeconds() {
        return cacheForSeconds;
    }
    public String getCoordinatorUrl() {
        if(coordinatorUrl == null && coordinatorContext != null){
            return managerContext.getCoordinatorAddress().getSoapAddress();
        }
        return coordinatorUrl;
    }
    public CoordinatorContext getCoordinatorContext() {
        return coordinatorContext;
    }
    public UnicastContianier getContainer() {
        return container;
    }

    public void setContainer(UnicastContianier container) {
        this.container = container;
    }

    public BaseContext getBaseContext() {
        return baseContext;
    }

    public void setCoordinatorUrl(String coordinatorUrl) {
        this.coordinatorUrl = coordinatorUrl;
    }

    public ValueConverter getConverter() {
        return converter;
    }

    public ChangeListener getChangeListener() {
        return changeListener;
    }

}

